package com.lzq.springboot_lzq.service;


import com.lzq.springboot_lzq.Repository.UserRepository;
import com.lzq.springboot_lzq.pojo.User;
import com.lzq.springboot_lzq.pojo.dto.UserDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {

    @Autowired
    UserRepository userRepository;

    @Override
    public User add(UserDto user) {
        User UserPojo = new User();
        BeanUtils.copyProperties(user,UserPojo);
        return userRepository.save(UserPojo);
        //调用数据访问
    }

    @Override
    public User findById(int id) {
        return userRepository.findById(id).orElseThrow( () -> {
            throw new IllegalArgumentException("用户不存在");
        });
    }

    @Override
    public User delete(int id) {
        userRepository.deleteById(id);
        return null;
    }

    @Override
    public Iterable<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public User edit(UserDto user) {
        User UserPojo = new User();
        BeanUtils.copyProperties(user,UserPojo);
        return userRepository.save(UserPojo);
    }
}
